package com.shengqi.xinnongren.work.ui.user.activities;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.utils.ImageUtils;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.goods.activities.GoodsDetailsActivity;
import com.shengqi.xinnongren.work.ui.user.model.CollectionBean;
import com.shengqi.xinnongren.work.ui.user.presenter.MyCollectionPresenter;
import com.shengqi.xinnongren.work.ui.user.view.MyCollectionView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.shengqi.xinnongren.work.MyApplication.getContext;

/**
 * 我的收藏
 */
public class MyCollectionActivity extends BaseMvpActivity<MyCollectionView, MyCollectionPresenter> implements MyCollectionView, SwipeMenuCreator, SwipeMenuItemClickListener {


    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView mSwipeMenuRecyclerView;

    private List<CollectionBean> mList;
    private CollectionAdapter mAdapter;

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_my_collection;
    }

    @Override
    protected void initView() {
        mList = new ArrayList<>();
        mAdapter = new CollectionAdapter();
        mSwipeMenuRecyclerView.setSwipeMenuCreator(this);
        mSwipeMenuRecyclerView.setSwipeMenuItemClickListener(this);
        mSwipeMenuRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration itemDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.custom_divider));
        mSwipeMenuRecyclerView.addItemDecoration(itemDecoration);
        mSwipeMenuRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.getCollectionList();

        mRefreshLayout.setOnRefreshListener(() -> mPresenter.getCollectionList());
    }

    @Override
    protected MyCollectionPresenter createPresenter() {
        return new MyCollectionPresenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        mRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showCollectionList(List<CollectionBean> list) {
        mRefreshLayout.setRefreshing(false);
        mList.clear();
        mList.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showDesCollection(String message) {
        ToastHelper.getInstance(this).showWarn(message);
        mPresenter.getCollectionList();
    }

    @Override
    public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
        int width = (int) getResources().getDimension(R.dimen.dp_70);
        int height = ViewGroup.LayoutParams.MATCH_PARENT;

        SwipeMenuItem deleteItem = new SwipeMenuItem(this)
                .setBackgroundColor(Color.parseColor("#83c120"))
                .setText("删除")
                .setTextSize(18)
                .setTextColor(Color.WHITE)
                .setWidth(width)
                .setHeight(height);
        swipeRightMenu.addMenuItem(deleteItem);
    }

    @Override
    public void onItemClick(SwipeMenuBridge menuBridge) {
        menuBridge.closeMenu();
        int position = menuBridge.getAdapterPosition();
        if (menuBridge.getDirection() == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
            //删除
            mPresenter.delCollection(mList.get(position).getComId());
        }
    }

    class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.Holder> {
        @NonNull
        @Override
        public MyCollectionActivity.CollectionAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            Holder holder = new Holder(getLayoutInflater().inflate(R.layout.item_collection, parent, false));
            holder.itemView.setOnClickListener(v -> GoodsDetailsActivity.start(MyCollectionActivity.this, mList.get(holder.getAdapterPosition()).getComId()));
            holder.ivAddCart.setOnClickListener(v -> Global.showSpecificationDialog(MyCollectionActivity.this, mList.get(holder.getAdapterPosition()).getComId()));
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyCollectionActivity.CollectionAdapter.Holder holder, int position) {
            CollectionBean goods = mList.get(position);
            ImageUtils.showImage(getContext(), goods.getMainPic(), holder.ivCover);
            holder.tvTitle.setText(goods.getComName());
            holder.tvDesc.setText(goods.getPhrase());
            holder.tvOriginMoney.setText(goods.getMarketPrice());
            holder.tvPrice.setText(goods.getPrice());
            holder.tvUnit.setText(String.format("/%s", goods.getUnitName()));
            //是否为新品
            if (goods.getIsNew() == 1) {
                holder.flIsNew.setVisibility(View.VISIBLE);
            } else {
                holder.flIsNew.setVisibility(View.GONE);
            }
            clearTagStyle(holder);
            showTagByLabel(goods.getLabel(), holder);
        }

        /**
         * 根据label 显示标签
         */
        private void showTagByLabel(List<Integer> label, Holder holder) {
            //1 满赠 2满减 3特价 4推荐 5限购 6热卖
            for (Integer integer : label) {
                switch (integer) {
                    case 1:
                        holder.mTvManZeng.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        holder.mTvManJian.setVisibility(View.VISIBLE);
                        break;
                    case 3:
                        holder.mTvTeJia.setVisibility(View.VISIBLE);
                        break;
                    case 4:
                        holder.mTvTuiJian.setVisibility(View.VISIBLE);
                        break;
                    case 5:
                        holder.mTvXianGou.setVisibility(View.VISIBLE);
                        break;
                    case 6:
                        holder.mTvReMai.setVisibility(View.VISIBLE);
                        break;
                }
            }
        }

        /**
         * 清除显示的标签
         */
        private void clearTagStyle(Holder holder) {
            holder.mTvManZeng.setVisibility(View.GONE);
            holder.mTvManJian.setVisibility(View.GONE);
            holder.mTvTeJia.setVisibility(View.GONE);
            holder.mTvTuiJian.setVisibility(View.GONE);
            holder.mTvXianGou.setVisibility(View.GONE);
            holder.mTvReMai.setVisibility(View.GONE);
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        public class Holder extends RecyclerView.ViewHolder {
            @BindView(R.id.fl_is_new)
            FrameLayout flIsNew;
            @BindView(R.id.tv_title)
            TextView tvTitle;
            @BindView(R.id.tv_desc)
            TextView tvDesc;
            @BindView(R.id.tv_price)
            TextView tvPrice;
            @BindView(R.id.tv_origin_money)
            TextView tvOriginMoney;
            @BindView(R.id.tv_unit)
            TextView tvUnit;
            @BindView(R.id.iv_cover)
            ImageView ivCover;
            @BindView(R.id.iv_add_cart)
            ImageView ivAddCart;
            @BindView(R.id.iv_collect)
            ImageView ivCollect;

            //1 满赠 2满减 3特价 4推荐 5限购 6热卖
            @BindView(R.id.tv_man_zeng)
            TextView mTvManZeng;
            @BindView(R.id.tv_man_jian)
            TextView mTvManJian;
            @BindView(R.id.tv_te_jia)
            TextView mTvTeJia;
            @BindView(R.id.tv_tui_jian)
            TextView mTvTuiJian;
            @BindView(R.id.tv_xian_gou)
            TextView mTvXianGou;
            @BindView(R.id.tv_re_mai)
            TextView mTvReMai;

            public Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
